package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglDatianMonitoring;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglDatianMonitoringMapper;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglDatianMonitoringService;
import org.jeecg.modules.demo.znnqjc.service.impl.DklistServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 大田监控
 * @Author: jeecg-boot
 * @Date:   2023-04-19
 * @Version: V1.0
 */
@Service
public class ZwglDatianMonitoringServiceImpl extends ServiceImpl<ZwglDatianMonitoringMapper, ZwglDatianMonitoring> implements IZwglDatianMonitoringService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DklistServiceImpl dklistService;
    @Autowired
    private ZwglDatianMonitoringMapper zwglDatianMonitoringMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String getjklist() {
        dklistService.getToken();
            try {
                String token = (String) redisTemplate.opsForValue().get("nstoken");
                String url = "http://112.102.225.89:51012/farming/openapi/getCameraData";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("accessToken", token);
                int responseCode = con.getResponseCode();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                JSONObject jsonObject = JSON.parseObject(String.valueOf(response));

                JSONArray jsonArray = new JSONArray(jsonObject.getJSONArray("data"));

                for (int j = 0; j <= jsonArray.size(); j++) {
                    JSONObject jsonObject2 = jsonArray.getJSONObject(j);
                    ZwglDatianMonitoring zwglDatianMonitoring=new ZwglDatianMonitoring();
                    zwglDatianMonitoring.setEquipmentId(jsonObject2.getString("id"));
                    zwglDatianMonitoring.setEquipmentNo(jsonObject2.getString("deviceSerial"));
                    zwglDatianMonitoring.setStationName(jsonObject2.getString("name"));
                    zwglDatianMonitoring.setVideoUrl(jsonObject2.getString("address"));
                    zwglDatianMonitoringMapper.insert(zwglDatianMonitoring);

                }

            } catch (Exception e) {
                System.out.println("Exception: " + e.getMessage());
            }


        return "ok";
    }
    @Override
    public String getyingshi() {

        //得到long类型当前时间
        long l = System.currentTimeMillis();
        //new日期对象
        Date date = new Date(l);
        //转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(date);
        //调用的api的接口地址
        String appKey = "16bcb0f3f89a46f48be32672270ac361";
        String appSecret = "c12ffe1edf7582e7c8a47afcc9216da6";
        String apiPath = "https://open.ys7.com/api/lapp/token/get?appKey=" + appKey + "&appSecret=" + appSecret;
        BufferedReader in = null;
        StringBuffer result = null;

        try {
            String accessToken1 = (String) redisUtil.get("accessToken");
            if (StringUtils.isEmpty(accessToken1)) {
                URL url = new URL(apiPath);
                //打开和url之间的连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestProperty("Charset", "utf-8");
                connection.connect();
                result = new StringBuffer();
                //读取URL的响应
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
                String result2 = result.toString(); //返回json字符串

                JSONObject jsonObject = JSON.parseObject(result2);

                JSONObject data = jsonObject.getJSONObject("data");

                String accessToken = (String) data.get("accessToken");

                //将token存入redis中
                redisUtil.set("accessToken", accessToken, 604800 );

                return accessToken;
            } else {
                System.out.println(accessToken1);

                return accessToken1;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
    @Override
    public Result getjk(){
        List<ZwglDatianMonitoring> list=new ArrayList<>();
        QueryWrapper<ZwglDatianMonitoring> queryWrapper=new QueryWrapper<>();
        list=zwglDatianMonitoringMapper.selectList(queryWrapper);
        return Result.OK(list);
    }
    @Override
    public Result getjkmc(String stationName){
        QueryWrapper<ZwglDatianMonitoring> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("station_name",stationName);
//        queryWrapper.last("limt 1");
        List<ZwglDatianMonitoring> list=zwglDatianMonitoringMapper.selectList(queryWrapper);
        return Result.OK(list);
    }
}
